package com.suxin.plugin.intergration.factory.process.pip.classs;

import com.suxin.plugin.intergration.factory.PluginRegistryInfo;
import com.suxin.plugin.intergration.factory.process.pip.PluginPipeProcessor;
import com.suxin.plugin.intergration.factory.process.pip.classs.group.ComponentGroup;
import com.suxin.plugin.intergration.factory.process.pip.classs.group.ConfigurationGroup;
import com.suxin.plugin.intergration.factory.process.pip.classs.group.ControllerGroup;
import com.suxin.plugin.loader.PluginResourceLoadFactory;
import com.suxin.plugin.loader.ResourceWrapper;
import com.suxin.plugin.loader.load.PluginClassLoader;
import com.suxin.plugin.pf4j.BasePlugin;
import org.springframework.core.io.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @classname PluginClassProcess
 * @description [ 对class进行分组 ]
 * @date 2022/7/21 21:28
 */
public class PluginClassProcess implements PluginPipeProcessor {

    private final List<PluginClassGroup> pluginClassGroup = new ArrayList<>();
    public final String OTHER = "spring_other";

    @Override
    public void initialize() throws Exception {
        pluginClassGroup.add(new ComponentGroup());
        pluginClassGroup.add(new ConfigurationGroup());
        pluginClassGroup.add(new ControllerGroup());
    }

    @Override
    public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception {
        BasePlugin basePlugin = pluginRegistryInfo.getPlugin();
        // 资源加载器
        PluginResourceLoadFactory pluginResourceLoadFactory = basePlugin.getPluginResourceLoadFactory();
        pluginResourceLoadFactory.load(basePlugin);
        ResourceWrapper resourceWrapper = pluginResourceLoadFactory.getResourceWrapper(PluginClassLoader.KEY);
        List<Resource> pluginResources = resourceWrapper.getResources();
        // 类组的初始化
        for (PluginClassGroup pluginClassGroup : pluginClassGroup) {
            pluginClassGroup.initialize(basePlugin);
        }
        // 分组类
        for (Resource pluginResource : pluginResources) {
            String path = pluginResource.getURL().getPath();
            String packageName = path.substring(0, path.indexOf(".class")).replace("/", ".");
            packageName = packageName.substring(packageName.indexOf(basePlugin.scanPackage()));
            Class<?> aClass = Class.forName(packageName, false, basePlugin.getPluginWrapper().getClassLoader());
            boolean findGroup = false;
            for (PluginClassGroup classGroup : pluginClassGroup) {
                if (classGroup.filter(aClass)) {
                    findGroup = true;
                    pluginRegistryInfo.addGroupClass(classGroup.groupId(), aClass);
                }
            }
            if (!findGroup) {
                pluginRegistryInfo.addGroupClass(OTHER, aClass);
            }
            // 添加到所有
            pluginRegistryInfo.addClasses(aClass);
        }
    }

    @Override
    public void unRegistry(PluginRegistryInfo pluginRegistryInfo) {

    }
}
